home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Atari Mega Archive 1
/
Atari Mega Archive - Volume 1.iso
/
language
/
examples.zoo
/
misc
/
files.lsp
< prev
next >
Wrap
Lisp/Scheme
|
1991-10-23
|
4KB
|
98 lines
;; Einige Utilities zum Umgang mit Files
; (COPY-FILE filename newname)
; wie (RENAME-FILE filename newname),
; nur daß das alte File unverändert bleibt und der Inhalt kopiert wird.
(defun copy-file (filename newname)
(let* ((oldpathname
(pathname (if (streamp filename) (truename filename) filename))
)
(newpathname (merge-pathnames newname oldpathname))
)
(with-open-file (istream oldpathname :element-type 'unsigned-byte :direction :input)
(with-open-file (ostream newpathname :element-type 'unsigned-byte :direction :output :if-exists :error)
(let* ((oldtruename (truename istream))
(newtruename (truename ostream))
(length (file-length istream))
(block-size
(let ((room (nth-value 1 (room))))
(when (or (> length 10000) (< room length)) (setq room (gc)))
(min length (round (* 0.95 room)))
) )
(block (make-string block-size))
)
(loop
(when (zerop length) (return))
(when (< length block-size) (setq block-size length))
(dotimes (i block-size)
(setf (schar block i) (int-char (read-byte istream)))
)
(dotimes (i block-size)
(write-byte (char-int (schar block i)) ostream)
)
(decf length block-size)
)
(values newpathname oldtruename newtruename)
) ) ) ) )
; (FILE->STRING file) liefert einen String mit dem File-Inhalt.
(defun file->string (file)
(with-open-file (s file :element-type 'string-char :direction :input)
(let ((eof "EOF")
(nl (string #\Newline))
(stringlist nil))
(loop
(multiple-value-bind (line terminated-by-eof) (read-line s nil eof)
(when (eq line eof) (return))
(push line stringlist)
(if (not terminated-by-eof) (push nl stringlist) (return))
) )
#+CLISP (apply #'string-concat (nreverse stringlist))
#-CLISP (apply #'concatenate 'string (nreverse stringlist))
) ) )
; (STRING->FILE filename string) baut ein File mit dem String als Inhalt.
(defun string->file (filename string)
(with-open-file (s filename :element-type 'string-char :direction :output)
(write-string string s)
(truename s)
) )
; (SHOW-FILE filename) zeigt den Inhalt eines Files hexadezimal an.
; Format jeder Zeile:
; 001230 20 21 22 23 24 25 26 27 28 29 2A 2B 2C 2D 2E 2F | !"#$%&'()*+,-./|
(defun show-file (filename &optional (start-position 0))
(with-open-file (s filename :element-type 'unsigned-byte :direction :input)
(file-position s start-position)
(let ((line-length 16) (i 0) position data)
(flet ((out-line ()
(let ((data (nreverse data)))
(format t "~% ~6,'0X ~{ ~2,'0X~}~VT|~{~A~}~V,0T|"
position data
(+ (* 3 line-length) 11)
(mapcar #'(lambda (x)
(let ((c (int-char x)))
(if (graphic-char-p c) c #\Space)
) )
data
)
(+ (* 4 line-length) 12)
)) ) )
(loop
(when (zerop i) (setq position (file-position s) data nil))
(let ((next (read-byte s nil nil)))
(if next
(progn
(push next data) (incf i)
(when (= i line-length) (out-line) (setq i 0))
)
(progn
(unless (zerop i) (out-line))
(return)
)
) ) )
) ) )
(values)
)